/*
 * Copyright 2010 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.resource {
	import org.splink.library.distributor.IDistributor;
	import org.splink.library.loading.QUrlLoader;
	import org.splink.library.queue.QEvent;
	import org.splink.library.queue.Qable;

	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	/**
	 * @inheritDoc
	 * 
	 * Boots splinkresource according to the <code>xml</code> file which is loaded 
	 * from the given path. This xml file must conform to resourcebundles.dtd 
	 * 
	 * @author Max Kugland
	 */
	final public class ExternalResourceProcessor extends Qable implements IResourceProcessor {
		private var _path : String;
		private var _locale : String;
		private var _loaderContext : LoaderContext;
		private var _loader : QUrlLoader;
		private var _processor : IResourceProcessor;

		/**
		 * @param path the path to the resourcebundles configuration xml
		 * @param locale the locale of the resourcebundle to load. If no valid locale is given the
		 * configured defaultLocale is used instead
		 */
		public function ExternalResourceProcessor(path : String, locale : String = "defaultLocale") {
			super(locale);
			_path = path;
			_locale = locale;
		}

		/**
		 * @inheritDoc
		 */
		public function loaderContext(loaderContext : LoaderContext) : IResourceProcessor {
			_loaderContext = loaderContext;
			return this;
		}

		/**
		 * @inheritDoc
		 */
		override protected function doStart() : void {
			_loader = new QUrlLoader(new URLRequest(_path));
			_loader.register(QEvent.COMPLETE, onComplete);
			_loader.register(QEvent.ERROR, onError);
			_loader.start();
		}

		private function onError(e : QEvent) : void {
			distribute(e.clone(this));
		}

		private function onComplete(e : QEvent) : void {
			process(new XML(QUrlLoader(e.source).getContent()));
		}

		private function process(xml : XML) : void {
			var scope : IDistributor = this;
			_processor = new XMLResourceProcessor(xml, _locale).loaderContext(_loaderContext);
			_processor.register(QEvent.COMPLETE, function(e : QEvent):void {
				complete();
			});
			_processor.register(QEvent.ERROR, function(e : QEvent):void {
				error(e.errorMessage);
			});
			_processor.register(QEvent.PROGRESS, function(e : QEvent):void {
				distribute(e.clone(scope));
			});
			_processor.register(ResourceProcessorEvent.PROGRESS, function(e : ResourceProcessorEvent):void {
				distribute(e.clone(scope));
			});
			
			_processor.start();
		}

		/**
		 * @inheritDoc
		 */
		override protected function doStop() : void {
			if(_loader) _loader.stop();			if(_processor) _processor.stop();
		}

		/**
		 * @inheritDoc
		 */
		override protected function doReset() : void {
			if(_loader) _loader.reset();			if(_processor) _processor.reset();
		}

		/**
		 * @inheritDoc
		 */
		override protected function doFinalize() : void {
			if(_loader) {
				_loader.finalize();
				_loader = null;
			}
			if(_processor) {
				_processor.finalize();
				_processor = null;
			}
			_loaderContext = null;
		}
	}
}
